package com.example.ldachu.testdownload.http.base;


import android.content.Context;
import android.widget.Toast;

import com.example.ldachu.testdownload.http.util.ToastUtil;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @author zxKueen on 2017-12-22 9:05
 *         Email: 4994766@qq.com
 *         有些异常不需要展示Toast,这些要重写onFail方法
 */

public abstract class BaseObserver<T> implements Observer<BaseResponse<T>> {


    private Context mContext;

    public BaseObserver(Context context) {
        mContext = context;
    }

    @Override
    public void onSubscribe(Disposable d) {

    }

    @Override
    public void onNext(BaseResponse<T> response) {
        if(response.isSuccess()){
            onSuccess(response);
        }else{
            //业务异常
            onFail(response.Status,response.Message);
        }
    }




    @Override
    public void onError(Throwable e) {
        //进行网络异常处理
        ToastUtil.show(mContext,"网络开小差了");
    }

    @Override
    public void onComplete() {

    }

    /**
     * 返回成功
     * @param response
     */
    public abstract void onSuccess(BaseResponse<T> response);

    //处理业务异常
    protected  void onFail(int status, String message){
        //通过业务码进行message修改
        //本地对业务码进行解析
        Toast.makeText(mContext, ""+message, Toast.LENGTH_SHORT).show();
    }
}
